﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToPLMX.TxObjectsHierarchy
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System.Collections.Generic;
using System.Linq;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToPLMX;

public class TxObjectsHierarchy
{
  private static Dictionary<string, TxObjData> m_hierarchy = new Dictionary<string, TxObjData>();

  internal static Dictionary<string, TxObjData> GetHierarchy() => TxObjectsHierarchy.m_hierarchy;

  internal static void CreateFlatHierarchy(List<string> idList)
  {
    HashSet<string> visited = new HashSet<string>();
    foreach (string id in idList)
    {
      ITxLocatableObject objectById = TxApplication.ActiveDocument.GetObjectById(id) as ITxLocatableObject;
      TxObjectsHierarchy.AddObject(objectById);
      TxObjectsHierarchy.RegisterAncestor((ITxObject) objectById, visited);
    }
    TxObjectsHierarchy.SetLevelOfObjects();
  }

  internal static void AddObject(ITxLocatableObject obj)
  {
    if (obj == null)
      return;
    TxObjData txObjData = (TxObjData) null;
    if (!TxObjectsHierarchy.m_hierarchy.TryGetValue(((ITxObject) obj).Id, out txObjData))
      txObjData = new TxObjData(obj, false, obj is ITxComponent);
    TxObjectsHierarchy.m_hierarchy[((ITxObject) obj).Id] = txObjData;
  }

  private static void RegisterAncestor(ITxObject child, HashSet<string> visited)
  {
    if (child == null || visited.Contains(child.Id))
      return;
    visited.Add(child.Id);
    TxObjectsHierarchy.RegisterPhysicalParent(child as ITxLocatableObject, visited);
  }

  private static void RegisterPhysicalParent(
    ITxLocatableObject locatableChild,
    HashSet<string> visited)
  {
    if (locatableChild == null || !(((ITxObject) locatableChild).Collection is ITxLocatableObject collection))
      return;
    TxObjectsHierarchy.AddChildToParent(collection, locatableChild, false);
    if (collection == TxApplication.ActiveDocument.PhysicalRoot)
      return;
    TxObjectsHierarchy.RegisterAncestor((ITxObject) collection, visited);
  }

  internal static void SetLevelOfObjects()
  {
    TxObjectsHierarchy.SetRecursiveLevel((ITxObject) TxApplication.ActiveDocument.PhysicalRoot, 0);
  }

  private static void SetRecursiveLevel(ITxObject obj, int level)
  {
    TxObjData txObjData = (TxObjData) null;
    if (obj == null || !TxObjectsHierarchy.m_hierarchy.TryGetValue(obj.Id, out txObjData))
      return;
    txObjData.level = level;
    if (txObjData.Children == null || !txObjData.Children.Any<string>())
      return;
    foreach (string child in txObjData.Children)
      TxObjectsHierarchy.SetRecursiveLevel(TxApplication.ActiveDocument.GetObjectById(child), level + 1);
  }

  internal static void AddChildToParent(
    ITxLocatableObject parent,
    ITxLocatableObject child,
    bool isLogical)
  {
    if (parent == null || child == null)
      return;
    TxObjData txObjData = (TxObjData) null;
    if (!TxObjectsHierarchy.m_hierarchy.TryGetValue(((ITxObject) parent).Id, out txObjData))
      txObjData = new TxObjData(parent, isLogical, parent is ITxComponent);
    if (!txObjData.Children.Contains(((ITxObject) child).Id))
      txObjData.Children.Add(((ITxObject) child).Id);
    TxObjectsHierarchy.m_hierarchy[((ITxObject) parent).Id] = txObjData;
  }
}
